package bid.yanhj.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import bid.yanhj.common.service.FormatService;

/**
 * 
 * @author yanhaijiang
 *
 */
public class NetUtil {
	
    private static final Logger LOG = LoggerFactory.getLogger(NetUtil.class);

    /**
     * http下载.
     * @param urlStr 下载地址
     * @param destFile 目标文件
     */
    public static void httpDownload(String urlStr, String destFile) {
        if (StringUtils.isBlank(urlStr) || StringUtils.isBlank(destFile)) {
            LOG.error(FormatService.logFormat("Params is null"));
            return;
        }
        
        URL url = null;
        URLConnection conn = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        
        try {
			url = new URL(urlStr);
			conn = url.openConnection();
			conn.connect();
			bis = new BufferedInputStream(conn.getInputStream());
			fos = new FileOutputStream(destFile);
			bos = new BufferedOutputStream(fos);
			byte[] bs = new byte[1024];
			int len = 0;
			while ((len = bis.read(bs)) != -1) {
			    bos.write(bs, 0, len);
			}
		} catch (MalformedURLException e) {
			LOG.error(FormatService.logFormat("MalformedURLException"), e);
		} catch (FileNotFoundException e) {
			LOG.error(FormatService.logFormat("FileNotFoundException"), e);
		} catch (IOException e) {
			LOG.error(FormatService.logFormat("IOException"), e);
		} finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                LOG.error(FormatService.logFormat("IOException"), e);
            }
		}
    }
    
    /**
     * 从FTP服务器下载文件.
     * 
     * @param host FTP服务器hostname
     * @param port FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param remotePath FTP服务器上的相对路径
     * @param fileName 要下载的文件名
     * @param localPath 下载后保存到本地的路径
     * @return 下载是否成功
     */
//    public static boolean ftpDownload(String host, int port, String username, String password, String remotePath,
//            String fileName, String localPath) {
//        boolean success = false;
//        FTPClient ftp = new FTPClient();
//        try {
//            int reply;
//            ftp.connect(host, port);
//            // 如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
//            ftp.login(username, password);// 登录
//            reply = ftp.getReplyCode();
//            if (!FTPReply.isPositiveCompletion(reply)) {
//                ftp.disconnect();
//                return success;
//            }
//            ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
//            FTPFile[] fs = ftp.listFiles();
//            if (fs != null && fs.length > 0) {
//                boolean bool = true;
//                for (FTPFile ff : fs) {
//                    if (ff.getName().equals(fileName)) {
//                        File localFile = new File(localPath + File.separator + ff.getName());
//                        OutputStream is = new FileOutputStream(localFile);
//                        ftp.retrieveFile(ff.getName(), is);
//                        is.flush();
//                        is.close();
//                        bool = false;
//                        break;
//                    }
//                }
//                if (bool) {
//                    LOG.error(FormatService.logFormat("下载文件不存在"));
//                }
//            }
//            ftp.logout();
//            success = true;
//        } catch (IOException e) {
//            LOG.error(FormatService.logFormat("IOException"), e);
//        } finally {
//            if (ftp.isConnected()) {
//                try {
//                    ftp.disconnect();
//                } catch (IOException e) {
//                    LOG.error(FormatService.logFormat("IOException"), e);
//                }
//            }
//        }
//        return success;
//    }
//    
    /**
     * 设置cookie.
     * 
     * @param response
     * @param name cookie名字
     * @param value cookie值
     * @param maxAge cookie生命周期 以秒为单位
     */
    public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        if (maxAge > 0) {
            cookie.setMaxAge(maxAge);
        }
        response.addCookie(cookie);
    }

    /**
     * 根据名字获取cookie.
     * 
     * @param request
     * @param name cookie名字
     * @return cookie值
     */
    public static String getCookieByName(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (null != cookies && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                return cookie.getValue();
            }
        }
        return null;
    }

    /**
     * 删除cookie.
     * 
     * @param response
     * @param name cookie名字
     */
    public static void delCookie(HttpServletResponse response, String name) {
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(-1);
        response.addCookie(cookie);
    }
}
